<?php

defined('MYLITS') ? '' : exit('No direct script access allowed');

/**
 * @version     1.0.0
 * @package     mylits engine
 * @license     GNU General Public License version 3
 * @author      Arfan Fudyartanto D N <arfan@mylits.com> - http://engine.mylits.com
 */
class Mylits_Process {

    public $params = array();
    private static $component = '';
    private static $component_path = '';
    public $template = '';
    public $html = '';
    public $base_url = '';

    public function __construct() {
        $Reflection = new ReflectionClass($this);
        $tmp = explode('/', str_replace('\\', '/', $Reflection->getFileName()));
        unset($tmp[count($tmp) - 1]);
        unset($tmp[count($tmp) - 1]);
        self::$component = end($tmp);
        self::$component_path = implode('/', $tmp) . '/';
        unset($tmp);
    }

    /**
     * Method to get component name
     * 
     * @return string component name
     */
    public function getComponentName() {
        return str_replace('comp.', '', self::$component);
    }

    /**
     * Method to get component path
     * 
     * @return string $path
     */
    public function getComponentPath() {
        return self::$component_path;
    }

    /**
     * Method to set template file
     * 
     * @param string $template, name of template {template_name}.tpl.php
     */
    public function setTemplate($template) {
        $this->template = $template;
        $this->base_url = BASE_URL . 'components/comp.' . $this->getComponentName() . '/';
    }

    /**
     * Method to load extended Mylits_Model
     * 
     * @param string $name -> model name, {model_name}.mdls.php
     * @return Mylits_Model object
     */
    public function loadModel($name) {
        $path = self::$component_path . 'models/' . $name . '.mdls.php';
        if (is_file($path)) {
            require_once $path;
            $class = $name . 'Model';
            $obj = new $class;
            $obj->component = self::$component;
            $obj->process = get_class($this);
            return $obj;
        } else {
            echo 'Model ' . self::$component . ' not found';
        }
    }

    /**
     * Method to get content of Mylits_Process object
     * 
     * @param array $data
     * @return string html view with extracted data
     */
    public function getContent($data) {
        $data['base_url'] = $this->base_url;
        $template_path = self::$component_path . 'templates/' . $this->template . '.tpl.php';
        if (is_file($template_path)) {
            $this->html = '<div class="view-' . $this->template . '">' . Mylits_Template::reader($template_path, $data) . "</div>";
        } else {
            exit('Template ' . $this->template . ' not found');
        }
        $temp = explode('.', self::$component);
        return '<div class="component"><div class="comp-' . end($temp) . '">' . $this->html . '</div></div>';
    }

    public function loadHelper($name) {
        $helper_path = self::$component_path . 'helpers/' . $name . '.hlpr.php';
        if (is_file($helper_path)) {
            require_once $helper_path;
        }
    }

}

?>
